/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.weatherEver.business.entity;

import it.polimi.weatherEver.business.control.access.PasswordEncrypter;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 *
 * @author Ettore
 */
@Entity (name="User")

public class User implements Serializable {
    private static long serialVersionUID = 1L;

    @Id
    @Pattern(regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
    message = "Invalid email")
    @NotNull(message = "May not be empty")
    private String emailAddress;
    @NotNull(message = "May not be empty")
    private String password;
    @NotNull
    private Boolean privacy;
    @OneToMany (mappedBy="owner")
    private List<Event> ownEventList;
    @OneToMany (mappedBy= "dest")
    private List<Notification> ownNotificationList;
    @ManyToMany
    @JoinTable(name= "scheduling",
            joinColumns= @JoinColumn (name="user_fk"),
            inverseJoinColumns= @JoinColumn (name= "event_fk")
    )
    private List<Event> eventListToEnjoy;
    @OneToMany (mappedBy="invited")
    private List<Invitation> invitationList;
    private String groupName;

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }



    /**
     * @return the serialVersionUID
     */
    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    /**
     * @return the emailAddress
     */
    public String getEmailAddress() {
        return emailAddress;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @return the privacy
     */
    public Boolean getPrivacy() {
        return privacy;
    }



    /**
     * @param aSerialVersionUID the serialVersionUID to set
     */
    public static void setSerialVersionUID(long aSerialVersionUID) {
        serialVersionUID = aSerialVersionUID;
    }

    /**
     * @param emailAddress the emailAddress to set
     */
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = PasswordEncrypter.encryptPassword(password);
    }

    /**
     * @param privacy the privacy to set
     */
    public void setPrivacy(Boolean privacy) {
        this.privacy = privacy;
    }



    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getEmailAddress() != null ? getEmailAddress().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.getEmailAddress() == null && other.getEmailAddress() != null) || (this.getEmailAddress()!= null && !this.getEmailAddress().equals(other.getEmailAddress()))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "it.polimi.weatherEver.business.entity.User[ id=" + getEmailAddress() + " ]";
    }
    
}
